Organising information: unordered structures

Communication 1

Video of previous lectures:

  1. Introduction to the course (alternative link)

  2. Introduction to Computational Thinking (alternative link)

  3. Algorithms (alternative link)

  4. Laboratory 1 (alternative link)

  5. Computability (alternative link)

  6. Programming languages (alternative link)

  7. Organising information - ordered structures (alternative link)

  8. Laboratory 2 (alternative link)

  9. Brute-force algorithms (alternative link)

  10. Laboratory 3 (alternative link)

Communication 2

Eats, shoots and leaves

Please, in your answers to the various exercises online, if you have to write a Python code, be sure that the correct indent is preserved by previewing your post before to publish it

You can use the ``` environment for defining your Python code:

```
write your Python code here
```

Communication 3

def f(n1, n2):
    r = n1 - n2
    return r + 1

Define a function: enclosing, under a specific name, a determinate sequence of steps that evaluate some ideal input to produce an ideal output

f(3, 4)
f(7, 2)
f(1, 8)

Execute a function: running the sequence of step by specifying some particular and concrete input to have a particular and concrete output in return

Communication 4

Clarification on test-driven development: all the tests must be passed in order to claim that an algorithm returns what it is expected

If a test execution return False, the test is not passed

If you need to check the non-compliancy of the execution of a function on purpose, then you have to create an additional testing function that returns True if the condition of the test is not passed

Communication 5

Writing the right tests is very important since it would allow you to catch wrong behaviours of functions in presence of specific input values

However, to use only one test is not enough

Test your function by using different kinds of input, including the unusual ones, e.g. empty strings or a list containing the same value twice or more times

Any question about the previous lecture?

Historic hero: Jorge Luis Borges

He was an Argentine short-story writer, poet, and essayist

He produced several works laying between philosophical literature and fantasy genre

Main topics of his works: dreams, labyrinths, libraries, mirrors, the notion of infinity, and religions

The Library of Babel

Big library, made of hexagonal rooms

  • 20 bookshelves in four of the walls of each room (5 bookshelves per wall)

  • Each bookshelf contains 35 books

  • Each book counts 410 pages

  • Each page organised in 40 lines

  • Each line contains 80 characters

The library contains all the books that have been and will be written by using every possible combination of 25 basic characters: 22 letters, the period, the comma, and the space

Does infinity exist?

Opening sentence: [the Library] is composed of an indefinite and perhaps infinite number of hexagonal galleries

The narrator suggests that the library is made of an infinite number of books, contained in an infinite number of rooms – however, is really this the case?

Mathematical infinity exists, it is an abstract concept: the set of all the prime numbers, which is an infinitive set

Often we refer to an infinite amount of something when actually we are speaking about a quite extensive and huge mass of stuff

What about the Library?

It contains only 2*101834097 of books

That number has been obtained by considering all the possible combination of all the finite set of charaters in all the 410 pages in all the books that can be generated

In existing computational systems (e.g. an electronic computer), we must be aware that infinity (e.g. the tape of a Turing Machine) is an illusion

Prelimilaries: classes

Class: extensible template for creating objects having a certain type (e.g. the class for strings, for integers, for lists)

Object: a value of a certain kind (e.g. "a string", 42, list([1,2,3]))

Method: a function that can be run only if directly called via an object (e.g. <list>.append(<item>))

Set: example

Set: definition

A set is a countable collection of unordered and non-repeatable elements

Countable: it is possible to know the length of the set (i.e. how many elements it contains) – in Python, we can use the function len(<countable_object>)

Unordered: the elements are placed in the set without any particular order

Unrepeatable: the elements cannot appear more than one time in the set

Set: methods

Create a new set: set()

Add new element: <set>.add(<element>)

Remove element: <set>.remove(<element>)

Add elements from another set: <set>.update(<another_set>)

Use methods on set

my_first_set = set()
my_first_set.add(34)
my_first_set.add(15)
my_first_set.add("Silvio")
my_first_set.remove(34)
my_first_set.update(my_first_set)

my_first_set = 34 15 "Silvio"

Dictionary: example

Dictionary: definition

A dictionary is a countable collection of unordered key-value pairs, where the key is non-repeatable in the dictionary

Unrepeatability of keys: the same key cannot be included twice in the dictionary

Dictionary: methods

Create a new dictionary: dict()

Add new pair: <dictionary>[<key>] = <value>

Remove a pair: del <dictionary>[<key>]

Get value by key: <dictionary>.get(<key>)

Add pairs from another dictionary: <dictionary>.update(<another_dictionary>)

Use methods on dictionary

my_first_dict = dict()
my_first_dict["age"] = 34
my_first_dict["day of birth"] = 15
my_first_dict["name"] = "Silvio"
del my_first_dict["age"]
my_first_dict.update(my_first_dict)
my_first_dict.get("name")

my_first_dict =

"name": "Silvio"
"day of birth": 15
"age": 34

Additional readings

From How To Code in Python:

  • Chapter "Understanding Data Types"
    section "Dictionaries"
  • Chapter "Understanding Dictionaries"
    all content
  • "How To Construct Classes and Define Objects"
    all content

END Organising information: unordered structures